package com.aiiter.house.service.impl;

import java.util.List;
import java.util.function.Consumer;

import com.aiiter.common.constant.PendingTaskConstants;
import com.aiiter.common.utils.DateUtils;
import com.aiiter.common.utils.SecurityUtils;
import com.aiiter.house.domain.vo.HousePendingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.aiiter.house.mapper.HousePendingTaskMapper;
import com.aiiter.house.domain.HousePendingTask;
import com.aiiter.house.service.IHousePendingTaskService;

/**
 * 待办任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class HousePendingTaskServiceImpl implements IHousePendingTaskService 
{
    @Autowired
    private HousePendingTaskMapper housePendingTaskMapper;

    /**
     * 查询待办任务
     * 
     * @param pendingId 待办任务主键
     * @return 待办任务
     */
    @Override
    public HousePendingTaskVo selectHousePendingTaskByPendingId(Long pendingId)
    {
        return housePendingTaskMapper.selectHousePendingTaskByPendingId(pendingId);
    }

    /**
     * 查询待办任务列表
     * 
     * @param task 待办任务
     * @return 待办任务
     */
    @Override
    public List<HousePendingTaskVo> selectHousePendingTaskList(HousePendingTask task)
    {
        // 管理员看到所有
        Long userId = SecurityUtils.getUserId();
        boolean admin = SecurityUtils.isAdmin(userId);
        if (admin) {
            userId=null;
        }
        task.setUserId(userId);
        return housePendingTaskMapper.selectHousePendingTaskList(task);
    }

    /**
     * 新增待办任务
     * 
     * @param pendingTask 待办任务 待办任务
     * @return 结果
     */
    @Override
    public int insertHousePendingTask(HousePendingTask pendingTask)
    {

        pendingTask.setCreateTime(DateUtils.getNowDate());
        Long userId = SecurityUtils.getUserId();
        pendingTask.setUserId(userId);
        pendingTask.setCreateBy(SecurityUtils.getUsername());
        pendingTask.setStatus(PendingTaskConstants.PENDING_TASK_STATUS_TO_DO);
        return housePendingTaskMapper.insertHousePendingTask(pendingTask);
    }

    /**
     * 修改待办任务
     * 
     * @param pendingTask 待办任务
     * @return 结果
     */
    @Override
    public int updateHousePendingTask(HousePendingTask pendingTask)
    {
//        pendingTask.setStatus(1);
        pendingTask.setUpdateTime(DateUtils.getNowDate());
        pendingTask.setUpdateBy(SecurityUtils.getUsername());
        return housePendingTaskMapper.updateHousePendingTask(pendingTask);
    }

    /**
     * 批量删除待办任务
     * 
     * @param pendingIds 需要删除的待办任务主键
     * @return 结果
     */
    @Override
    public int deleteHousePendingTaskByPendingIds(Long[] pendingIds)
    {
        return housePendingTaskMapper.deleteHousePendingTaskByPendingIds(pendingIds);
    }

    /**
     * 删除待办任务信息
     * 
     * @param pendingId 待办任务主键
     * @return 结果
     */
    @Override
    public int deleteHousePendingTaskByPendingId(Long pendingId)
    {
        return housePendingTaskMapper.deleteHousePendingTaskByPendingId(pendingId);
    }


    /**
     * 凌晨1点执行 前一天的待完成任务刷为已过期
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void deleteExpiredPendingTask() {
        // 查询前一天的待完成任务
        List<HousePendingTask>  expiredPendingTask = housePendingTaskMapper.selectExpiredPendingTask();
        expiredPendingTask.forEach(new Consumer<HousePendingTask>() {
            @Override
            public void accept(HousePendingTask task) {
                // 置为已过期
                task.setStatus(PendingTaskConstants.PENDING_TASK_STATUS_EXPIRED);
                task.setUpdateTime(DateUtils.getNowDate());
                task.setUpdateBy(SecurityUtils.getUsername());
                housePendingTaskMapper.updateHousePendingTask(task);
            }
        });
    }
}
